package com.hnevc.mallserver3.controller;

import com.hnevc.mallserver3.common.ResponseResult;
import com.hnevc.mallserver3.dto.CartDTO;
import com.hnevc.mallserver3.entity.Cart;
import com.hnevc.mallserver3.service.CartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/cart")
@Api(tags = "购物车信息接口")
public class CartController {
    @Autowired
    private CartService cartService;

    @GetMapping("/memberId/{memberId}")
    @ApiOperation(value = "getCartItemsByMemberId", notes = "根据ID获取购物车信息")

    public ResponseResult getCartItemsByMemberId(@PathVariable Long memberId) {
        List<CartDTO> cartItems = cartService.getCartItemsByMemberId(memberId);
        return ResponseResult.success(cartItems);
    }

    @PostMapping("/add")
    @ApiOperation(value = "addCart", notes = "添加购物车")
    public ResponseResult addCart(@RequestBody Cart cart) {
        cartService.save(cart);
        return ResponseResult.success(null); // 或者返回一些添加成功的信息
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "getCartById", notes = "根据ID找购物车信息")
    public ResponseResult getCartById(@PathVariable Long id) {
        Cart cart = cartService.getById(id);
        return ResponseResult.success(cart);
    }

    @GetMapping("/list")
    @ApiOperation(value = "listCarts", notes = "获取所有购物车信息")
    public ResponseResult listCarts() {
        List<Cart> carts = cartService.list();
        return ResponseResult.success(carts);
    }

    @PutMapping("/update")
    @ApiOperation(value = "updateCart", notes = "更新购物车信息")

    public ResponseResult updateCart(@RequestBody Cart cart) {
        cartService.updateById(cart);
        return ResponseResult.success(null); // 或者返回一些更新成功的信息
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "deleteCart", notes = "根据ID删除购物车记录")

    public ResponseResult deleteCart(@PathVariable Long id) {
        cartService.removeById(id);
        return ResponseResult.success(null); // 或者返回一些删除成功的信息
    }
}
